<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-search-group">
                        <div class="cms-left-btn">
                            <el-button type="primary" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                            <el-button type="primary" icon="fa fa-plus" @click="create" size="small"> 新增</el-button>
                        </div>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input  size="medium" @keydown.enter.native="list(1)" v-model="params.kwd" placeholder="请输入账号、名称或者标识" class="input-with-select">
                                <el-button type="primary" @click="list(1)" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData.data" border>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column align="center" prop="applet_type" label="类别" width="70">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.applet_type == 'article'">美文</el-tag>
                                <el-tag v-if="scope.row.applet_type == 'video'" type="success">视频</el-tag>
                                <el-tag v-if="scope.row.applet_type == 'photo'" type="warning">相册</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="title" label="名称" width="180"></el-table-column>
                        <el-table-column prop="name" label="标识" width="180"></el-table-column>
                        <el-table-column prop="account" label="账号" width="180"></el-table-column>
                        <el-table-column prop="appid" label="APPID" width="180"></el-table-column>
                        <el-table-column prop="manages" label="负责人" width="180"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <router-link :to="{path:scope.row.name}" target="_blank" style="margin-right: 10px">
                                    <el-button type="primary" icon="fa fa-sign-in" size="mini" plain>
                                        登录
                                    </el-button>
                                </router-link>
                                <el-button type="primary" size="mini" plain @click="edit(scope.row)" icon="fa fa-edit">
                                    编辑
                                </el-button>
                                <el-button type="warning" size="mini" plain icon="fa fa-bar-chart" @click="routerAppletCount(scope.row.name)">
                                    统计
                                </el-button>
                                <el-button icon="fa fa-trash" type="danger" @click="destroy(scope.row.id)" size="mini" plain>
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item label="类型" required prop="applet_type">
                    <el-radio-group  v-model="form.applet_type" :disabled="form.applet_type_off">
                        <el-radio border label="article">美文</el-radio>
                        <el-radio border label="video">视频</el-radio>
                        <el-radio border label="photo">相册</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序名称', trigger: 'blur'}]" label="名称"  prop="title">
                    <el-input style="width: 100%;" v-bind:content="form.title"
                              v-model="form.title" placeholder="请填写小程序名称"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序标识', trigger: 'blur'}]" label="标识"  prop="name">
                    <el-input style="width: 100%;" v-bind:content="form.name"
                              v-model="form.name" placeholder="请填写小程序标识"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序账号', trigger: 'blur'}]" label="账号"  prop="account">
                    <el-input style="width: 100%;" v-bind:content="form.account"
                              v-model="form.account" placeholder="请填写小程序账号"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序appid', trigger: 'blur'}]" label="APPID"  prop="appid">
                    <el-input style="width: 100%;" v-bind:content="form.appid"
                              v-model="form.appid" placeholder="请填写小程序appid"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序secret', trigger: 'blur'}]" label="SECRET"  prop="secret">
                    <el-input style="width: 100%;" v-bind:content="form.secret"
                              v-model="form.secret" placeholder="请填写小程序secret"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序token', trigger: 'blur'}]" label="TOKEN"  prop="token">
                    <el-input style="width: 100%;" v-bind:content="form.token"
                              v-model="form.token" placeholder="请填写小程序token"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写小程序aeskey', trigger: 'blur'}]" label="AESKEY"  prop="aeskey">
                    <el-input style="width: 100%;" v-bind:content="form.aeskey"
                              v-model="form.aeskey" placeholder="请填写小程序aeskey"></el-input>
                </el-form-item>
                <el-form-item label="负责人"  prop="manages">
                    <el-input style="width: 100%;" v-bind:content="form.manages"
                              v-model="form.manages" placeholder="请填写小程序负责人"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update('form')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<!---->
<script>
    import {mapGetters} from 'vuex';
    import {AppletList, AppletUpdate} from '@/utils/request';
    import Pagination from '@/components/pagination';

    export default {
        components: {Pagination},
        data() {
            return {
                params: {
                    page: 1,kwd:''
                },
                listData: [],
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '550px'
                },
                form:null,
            }
        },
        activated() {
            this.list();
        },
        computed: {
            ...mapGetters(['active', 'loading']),
        },
        methods: {
            //列表
            list(page) {
                if (page) {
                    this.params.page = page;
                }
                this.$store.dispatch('setLoading', true);
                AppletList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },

            //新增
            create(){
                this.form = {
                    id:0,
                    name:'',
                    title: '',
                    account:'',
                    appid:'',
                    secret:'',
                    token:'',
                    aeskey:'',
                    manages: '',
                    applet_type: 'article',
                    applet_type_off: false
                };
                this.dialog.title = '新增小程序';
                this.dialog.show = true;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    name: info.name,
                    title: info.title,
                    account: info.account,
                    appid: info.appid,
                    secret: info.secret,
                    token: info.token,
                    aeskey: info.aeskey,
                    manages: info.manages,
                    applet_type: info.applet_type,
                    applet_type_off: true
                };
                this.dialog.title = '编辑小程序';
                this.dialog.show = true;
            },
            update(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        AppletUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            routerAppletCount(name) {
                this.$store.dispatch('setCountAppletActive',name).then(()=>{
                    let routeUrl = this.$router.resolve({
                        name: "AppletCountVisites"
                    });
                    window.open(routeUrl .href, '_blank');
                })

            }
        }
    }
</script>

<style scoped>
    .cms-left-btn {
        float: left;
    }

    .el-tab-pane {
        display: none;
    }
</style>
